#include "DeleteRegionAction.hpp"

DeleteRegionAction::DeleteRegionAction(EditorRegionManager* regionManager, RegionPanel* panel, Region* r){
    regionPanel = panel;
    manager = regionManager;
    region = r;

    desc = "Deleted Region '" + std::string(region->name) + "'";
    perform();
}

void DeleteRegionAction::perform(){
    //Create region backup
    Region* deleteRegion = region;
    region = new EditorRegion(deleteRegion->x, deleteRegion->y, deleteRegion->width, deleteRegion->height);
    region->setName(deleteRegion->name);
    region->setScript(deleteRegion->script);
    //Delete region and remove from panel.
    regionPanel->removeRegion((EditorRegion*)deleteRegion);
    manager->destroyRegion(deleteRegion);
}

void DeleteRegionAction::undo(){
    Region* newRegion = manager->createRegion(region->name);
    newRegion->setArea(region->x, region->y, region->width, region->height);
    newRegion->setScript(region->script);
    newRegion->width = region->width;
    newRegion->height = region->height;

    //Delete backup
    delete region;

    region = newRegion;
    regionPanel->addRegion((EditorRegion*)region);
}